package com.wffz.controller;

import com.github.pagehelper.PageInfo;
import com.wffz.comm.api.ResponseResult;
import com.wffz.comm.utils.DateUtils;
import com.wffz.comm.utils.SignAPI;
import com.wffz.comm.utils.SignUtil;
import com.wffz.dto.MoneyDTO;
import com.wffz.dto.WithDrawDTO;
import com.wffz.mapper.MoneyLogMapper;
import com.wffz.po.*;
import com.wffz.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.*;

/**
 * @author sunlei
 * @className MoneyController
 * @Description
 * @date 2023-02-05 10:00
 **/
@RestController
@Slf4j
@RequestMapping("/money")
public class MoneyController {

    @Resource
    private MoneyService moneyService;

    @Resource
    private MoneyLogService moneyLogService;

    @Resource
    private UserService userService;

    @Resource
    private WithDrawRecordService recordService;

    @Resource
    private UserCollectBankService userCollectBankService;


    /**
     * @Description: [充值]
     * @Title: recharge
     * @Author: SUNLEI
     * @Date: 2023-02-27
     * @Param: moneyDTO
     * @Param: request
     * @Return: com.wffz.comm.api.ResponseResult
     * @Throws:
     */
    @PostMapping("/recharge")
    public ResponseResult recharge(@RequestBody MoneyDTO moneyDTO, HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        String payUrl = moneyService.recharge(clientUser, moneyDTO);
        if (Objects.nonNull(payUrl)){
            return ResponseResult.success(payUrl);
        }else {
            return ResponseResult.error();
        }

    }


    @GetMapping("/getMoneyLogTopThree")
    public ResponseResult getMoneyLogTopThree() {
        List<MoneyLog> list = moneyService.getMoneyLogTop3();
        return ResponseResult.success(list);
    }

    @RequestMapping("/getMoneyLog")
    public ResponseResult getMoneyLog(@RequestParam(value = "pageNum", required = true, defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", required = true, defaultValue = "1") Integer pageSize,
                                      HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        PageInfo<MoneyLog> pageInfo = moneyService.queryMoneyLog(pageNum, pageSize, clientUser.getLoginName());
        return ResponseResult.success(pageInfo);
    }

    /**
     * @Description: [提现]
     * @Title: withDraw
     * @Author: SUNLEI
     * @Date: 2023-04-22
     * @Param: withDrawDTO
     * @Param: request
     * @Return: com.wffz.comm.api.ResponseResult
     * @Throws:
     */
    @PostMapping("/withDraw")
    public ResponseResult withDraw(@RequestBody WithDrawDTO withDrawDTO, HttpServletRequest request) {
        WithdrawRecord record = new WithdrawRecord();
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        int vipLevel = clientUser.getVipLevel();
        float amount = withDrawDTO.getAmount();
        // 获取提现总额  根据总额计算
        // 获取充值总金额
        Float moneySum = moneyLogService.sumMoney(clientUser);
        //
        boolean isCan = true;
        switch (vipLevel) {
            case 1:
                if (amount > moneySum * 1.25) {
                    isCan = false;
                }
                break;
            case 2:
                if (amount > moneySum * 1.40) {
                    isCan = false;
                }
                break;
            case 3:
                if (amount > moneySum * 1.45) {
                    isCan = false;
                }
                break;
            case 4:
                if (amount > moneySum * 1.50) {
                    isCan = false;
                }
                break;
            case 5:
                if (amount > moneySum * 1.60) {
                    isCan = false;
                }
                break;
            case 6:
                if (amount > moneySum * 1.80) {
                    isCan = false;
                }
                break;
            case 7:
                if (amount > moneySum * 2.00) {
                    isCan = false;
                }
                break;
            case 8:
                isCan = true;
                break;
        }
        //
        if (!isCan) {
            return ResponseResult.success(false);
        }
        // 新增提现记录
        UserCollectBank userCollectBank = userCollectBankService.getBankById(withDrawDTO.getBankId());
        record.setUserId(clientUser.getId());
        record.setLoginName(clientUser.getLoginName());
        record.setBankId(withDrawDTO.getBankId());
        record.setAmount(withDrawDTO.getAmount());
        record.setCreateTime(DateUtils.getNowDate());
        record.setBankName(userCollectBank.getBankName());
        record.setBankAccount(userCollectBank.getBankAccount());
        record.setStatus("0");
        // 先扣除费用，扣除之后审核，如果审核失败，再把钱返回
        clientUser.setBalance(clientUser.getBalance() - withDrawDTO.getAmount());
        userService.updateClientUser(clientUser);
        return ResponseResult.success(recordService.saveWithDrawRecord(record));
    }


    @RequestMapping("/getWithDrawRecord")
    public ResponseResult getWithDrawRecord(@RequestParam(value = "pageNum", required = true, defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize", required = true, defaultValue = "1") Integer pageSize,
                                            HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        PageInfo<WithdrawRecord> pageInfo = recordService.getWithDrawRecord(pageNum, pageSize, clientUser.getLoginName());
        return ResponseResult.success(pageInfo);
    }

}
